package com.ray.depot.service.impl;

import org.apache.log4j.Logger;
import org.flyong.cache.query.cache.CacheConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.depot.service.SupplierService;
import com.ray.depot.table.entity.Supplier;
import com.ray.depot.table.entity.SupplierExample;
import com.ray.depot.table.entity.SupplierExample.Criteria;
import com.ray.depot.table.mapper.SupplierMapper;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class SupplierServiceImpl extends BaseService implements SupplierService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(SupplierServiceImpl.class);
	
	@Autowired
	private SupplierMapper supplierMapper;

	@Transactional
	@CacheConfig(paramsSize=1,add=true,id="supplierId", clearKey="supplierList")
	public Result<Object> addSupplier(Supplier params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setSupplierId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=supplierMapper.insertSelective(params);
		if(count!=1){
			logger.error("SupplierServiceImpl=>addSupplier is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	@CacheConfig(key="supplier",paramsSize=1,query=true,expire=1800)
	public Supplier querySupplier(String id, LoginUser user) {
		return supplierMapper.selectByPrimaryKey(id);
		}
	
	
    @Transactional
	@CacheConfig(key="supplier",paramsSize=1,update=true,id="supplierId", clearKey="supplierList")
	public Result<Object> updateSupplier(Supplier params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=supplierMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("SupplierServiceImpl=>editSupplier is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	@CacheConfig(key="supplier",paramsSize=1,delete=true, clearKey="supplierList")
	public Result<Object> deleteSupplier(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		Supplier params=new Supplier();
		params.setSupplierId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=supplierMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("SupplierServiceImpl=>deleteSupplier is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	@CacheConfig(key="supplierList",query=true,paramsSize=2,expire=1800)
	public PageDto<Supplier> querySupplier(Supplier params, PageDto<Supplier> page, LoginUser user) {
		SupplierExample example=new SupplierExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		//名称
		if(StrUtil.isNotBlank(params.getSupplierName())){
			criteria.andSupplierNameLike(params.getSupplierName()+"%");
		}
		
		//助记码
		if(StrUtil.isNotBlank(params.getMnemonicCode())){
			criteria.andMnemonicCodeLike(params.getMnemonicCode()+"%");
		}
		
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(supplierMapper.countByExample(example));
		page.setRecords(supplierMapper.selectByExample(example));
		return page;
	}

}
